import { Component, Enum, IVec3Like, Node, Vec3, _decorator } from 'cc';
import { Vector3 } from '../../../Framework/Scripts/Utils/Vector3';
import { GameApp } from '../GameApp';
// import { Quaternion } from './Quaternion';
// import { VectorTool } from './VectorTool';
const { ccclass, property } = _decorator;

export enum ThirdPersonCameraType {
    /** 相机紧跟随着目标，相机不会旋转 */
    Follow = 0,
    /** 相机会旋转紧跟着目标正后方，旋转不可控制 */
    FollowTrackRotation = 1,
    /** 相机紧跟随着目标，相机可以自由旋转 */
    FollowIndependentRotation = 2,
}

/**
 * 第三人称相机跟随
 * 这里总结了三个相机跟随
 * 1. 相机紧跟随着目标，相机不会旋转
 * 2. 相机紧跟随着目标，相机会旋转紧跟着目标正后方，旋转不可控制
 * 3. 相机紧跟随着目标，相机可以自由旋转，角色向前移动的时候，前方向永远是相机的正方向
 */
@ccclass('ThirdFreeLookCamera')
export class ThirdFreeLookCamera extends Component {
    /** 目标 */

    public target: Node = null;
    /** 注视的目标，这里我想让相机对准目标的上方一点，所有多加了注视（相机正对着）的目标 */

    public lookAt: Node = null;
    @property({ type: Enum(ThirdPersonCameraType) })
    cameraType: ThirdPersonCameraType = ThirdPersonCameraType.Follow;
    /** 距离目标距离 */
    @property
    positionOffset: Vec3 = new Vec3(0, 120, 200);
    /** 移动差值移动系数 */
    @property
    moveSmooth: number = 0.02;
    /** 差值旋转系数 */
    @property
    rotateSmooth: number = 0.03;

    public MouseX: number = 0;
    public MouseY: number = 0;

    private _forward: Vec3 = new Vec3();
    private _right: Vec3 = new Vec3();
    private _up: Vec3 = new Vec3();
    private angle: IVec3Like = null;

    //private startPos: Vec3 = new Vec3()
    private offsetPos: Vec3 = new Vec3()

    public init(player: Node, lookNode: Node) {
        this.node.setWorldPosition(GameApp.Instance.CamerStartPos)//复位
        this.target = player
        this.lookAt = lookNode
      //  this.startPos = this.node.getWorldPosition()
        if(this.target){
            this.offsetPos = Vector3.sub(this.node.getWorldPosition(), this.target.getWorldPosition())

        }
        // this.cameraType == ThirdPersonCameraType.Follow && this.node.lookAt(this.target.worldPosition);

    }

    start() {

    }

    isDown: boolean = false;
    // private MouseDown(e: EventMouse) {
    //     this.isDown = true;
    // }

    // private MouseMove(e: EventMouse) {

    //     if (this.cameraType == ThirdPersonCameraType.FollowIndependentRotation) {

    //         this.SetIndependentRotation(e);
    //     }

    // }

    // private MouseUp(e: EventMouse) {
    //     this.isDown = false;
    // }

    update(dt: number) {
     
    }
    lateUpdate(){
        if (this.target) {
            switch (this.cameraType) {
                case ThirdPersonCameraType.Follow:
                    this.SetFollow();
                    break;
                case ThirdPersonCameraType.FollowTrackRotation:
                    //  this.SetFollowTrackRotation();
                    break;
                case ThirdPersonCameraType.FollowIndependentRotation:
                    this.SetMove();
                    break;
            }

        }
    }

    private SetFollow() {
        // this.node.lookAt(this.target.worldPosition);
        // //    console.log("1摄像机",this.node.eulerAngles);

        // let temp: Vec3 = new Vec3();
        // Vec3.add(temp, this.lookAt.worldPosition, new Vec3(this.positionOffset.x, this.positionOffset.y, this.positionOffset.z));
        // //   this.node.position = this.node.position.lerp(temp, this.moveSmooth);
        // this.node.position = temp

        let temp: Vec3 = new Vec3();
        Vec3.add(temp, this.lookAt.worldPosition, this.offsetPos);
        this.node.setPosition(this.node.getPosition().lerp(temp, this.moveSmooth)) ;

       // this.node.position = temp

    }

    private velocity = new Vec3();
    private forwardView: Vec3 = new Vec3();
    /**
     * 
     */
    // private SetFollowTrackRotation() {
    //     //这里计算出相机距离目标的位置的所在坐标先，距离多高Y，距离多远Z
    //     //下面四句代码等同于：targetPosition+Up*updistance-forwardView*backDistance
    //     let u = Vec3.multiplyScalar(new Vec3(), Vec3.UP, this.positionOffset.y);
    //     let f = Vec3.multiplyScalar(new Vec3(), this.target.forward, this.positionOffset.z);
    //     let pos = Vec3.add(new Vec3(), this.target.position, u);
    //     //本来这里应该是减的，可是下面的lookat默认前方是-z，所有这里倒转过来变为加
    //     Vec3.add(pos, pos, f);
    //     //球形差值移动，我发现cocos只有Lerp差值移动，而我看unity是有球形差值移动的，所有我这里照搬过来了一个球形差值
    //     this.node.position = VectorTool.SmoothDampV3(this.node.position, pos, this.velocity, this.moveSmooth, 100000, 0.02);
    //     //cocos的差值移动
    //     //this.node.position=this.node.position.lerp(pos,this.moveSmooth);
    //     //计算前方向
    //     this.forwardView = Vec3.subtract(this.forwardView, this.node.position, this.target.getWorldPosition());
    //     this.node.lookAt(this.target.worldPosition);
    //     //this.node.rotation=Quaternion.LookRotation(this.forwardView);
    // }

    /*************************FollowIndependentRotation***************** */

    /**
     * 实时设置相机距离目标的位置position
     */
    public SetMove() {
        this._forward = new Vec3();
        this._right = new Vec3();
        this._up = new Vec3();
        Vec3.transformQuat(this._forward, Vec3.FORWARD, this.node.rotation);
        Vec3.transformQuat(this._right, Vec3.RIGHT, this.node.rotation);
        Vec3.transformQuat(this._up, Vec3.UP, this.node.rotation);

        this._forward.multiplyScalar(this.positionOffset.z);
        this._right.multiplyScalar(this.positionOffset.x);
        this._up.multiplyScalar(this.positionOffset.y);
        let desiredPos = new Vec3();
        desiredPos = desiredPos.add(this.lookAt.worldPosition).subtract(this._forward).add(this._right).add(this._up);
        this.node.position = this.node.position.lerp(desiredPos, this.moveSmooth);
    }


    /**
     * 计算根据鼠标X，Y偏移量来围绕X轴和Y轴的旋转四元数
     * @param e 
     */
    // private SetIndependentRotation(e: EventMouse) {

    //     let radX: number = -e.movementX;
    //     let radY: number = -e.movementY;
    //     let _quat: Quat = new Quat();

    //     //计算绕X轴旋转的四元数并应用到node，这里用的是鼠标上下Y偏移量
    //     let _right = Vec3.transformQuat(this._right, Vec3.RIGHT, this.node.rotation);
    //     _quat = Quaternion.RotationAroundNode(this.node, this.target.position, _right, radY);
    //     this.angle = Quaternion.GetEulerFromQuat(_quat);
    //     //限制相机抬头低头的范围
    //     this.angle.x = this.angle.x > 0 ? this.Clamp(this.angle.x, 120, 180) : this.Clamp(this.angle.x, -180, -170);
    //     Quat.fromEuler(_quat, this.angle.x, this.angle.y, this.angle.z);
    //     this.node.setWorldRotation(_quat);

    //     //计算绕Y轴旋转的四元数并应用到node，这里用的是鼠标上下X偏移量
    //     _quat = Quaternion.RotationAroundNode(this.node, this.target.position, Vec3.UP, radX);
    //     this.node.setWorldRotation(_quat);

    //     this.angle = Quaternion.GetEulerFromQuat(_quat);
    //     this.MouseX = this.angle.y;
    //     this.MouseY = this.angle.x;
    //     //console.log(this.MouseX.toFixed(2),this.MouseY.toFixed(2));
    // }

    /*************************FollowIndependentRotation end***************** */

    // private Clamp(val: number, min: number, max: number) {
    //     if (val <= min) val = min;
    //     else if (val >= max) val = max;
    //     return val;
    // }

    public GetType(): ThirdPersonCameraType {
        return this.cameraType;
    }
}



